<?php

/**
 * default actions.
 *
 * @package    Amman to London
 * @subpackage default
 * @author     Adel Abu-Saif <adeljas@msn.com>
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class defaultActions extends sfActions {

    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $request) {
        
    }

    public function executeLogin(sfWebRequest $request) {

        if ($this->getUser()->isSuperAdmin()) {
            sfContext::getInstance()->getConfiguration()->loadHelpers(array('Url'));
            $this->redirect(public_path('/admin.php/', true));
        }
        if (!$this->getUser()->isAuthenticated()) {

            $facebook = new Facebook(array(
                        'appId' => sfConfig::get('app_facebook_app_id'),
                        'secret' => sfConfig::get('app_facebook_app_secret'),
                    ));

            $userId = $facebook->getUser();
        }

        if (isset($userId) && $userId != 0 && !$this->getUser()->isAuthenticated()) {
            $userInfo = $facebook->api('/' + $userId);

            $q = sfGuardUserTable::getInstance()->createQuery()->where('username = ?', $userId);
            $c = $q->count();

            if ($c == 0) {
                $gu = new sfGuardUser();
                $gu->setUsername($userId);
                $gu->setFirstName($userInfo['first_name']);
                $gu->setLastName($userInfo['last_name']);
                $gu->setEmailAddress($userInfo['email'] . ' ');
                $gu->setPassword(sha1(time()));
                $gu->setIsActive(1);
                $gu->save();
                /* save picture */
                file_put_contents(sfConfig::get('sf_upload_dir') . '/profile_pictures/' . $userId . '.jpg', file_get_contents('http://graph.facebook.com/' . $userId . '/picture?type=large'));
                copy(sfConfig::get('sf_upload_dir') . '/profile_pictures/' . $userId . '.jpg', sfConfig::get('sf_upload_dir') . '/profile_pictures/' . $userId . '_thumb.jpg');
            } else {
                $gu = $q->fetchOne();
            }
            $this->getUser()->signin($gu);
            $this->redirect('@homepage');
        }


        $this->form = new sfGuardFormSignin();

        $this->setLayout('login');
        $this->create_account_form = new sfGuardRegisterForm();
        $this->create_account_form->getValidator('first_name')->setOption('required', true);
        unset($this->create_account_form['last_name']);
        //unset($this->create_account_form['username']);

        if ($request->isMethod('POST')) {
            $values = $request->getParameter($this->create_account_form->getName());
            $values['username'] = $values['email_address'];
            $this->create_account_form->bind($values);
            if ($this->create_account_form->isValid()) {

                sfContext::getInstance()->getConfiguration()->loadHelpers(array('global', 'Partial'));

                $gu = $this->create_account_form->save();

                $email_params = array(
                    'from' => sfConfig::get('app_default_email')
                    , 'to' => $gu->getEmailAddress()
                    , 'subject' => 'Doseyeh membership activation'
                );

                $template_vars = array(
                    'salt' => $gu->getSalt()
                    , 'name' => $gu->getFirstName()
                );

                send_email('user_activation', $email_params, $template_vars);

              //  $this->getUser()->setFlash('notice', 'Please check your email to proceed with registration process');
                //$this->getUser()->signin($gu);
                $this->redirect('success_message',array("message"=>"Please check your email to proceed with registration process"));
            } else {
                //  die($this->form);
            }
        }
        
        $this->users_pictures = glob(sfConfig::get('sf_upload_dir').'/profile_pictures/*_thumb.jpg');
        
        shuffle($this->users_pictures);
        
    }

}
